import pandas as pd
import mplfinance as mpf
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname='simhei.ttf', size=12)

# 1. 准备测试数据
data = pd.DataFrame({
    'Open': [1, 2, 3, 4, 5],
    'High': [1.5, 2.5, 3.5, 4.5, 5.5],
    'Low': [0.5, 1.5, 2.5, 3.5, 4.5],
    'Close': [1.2, 2.3, 3.2, 4.1, 5.3],
    'Volume': [100, 200, 150, 300, 250]
}, index=pd.date_range('2023-01-01', periods=5))

# 2. 设置中文字体（解决乱码关键步骤）
plt.rcParams['font.sans-serif'] = ['SimHei']  # Windows系统使用黑体
# plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']  # Mac系统使用
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

# 3. 绘制K线图并测试中文
style = mpf.make_mpf_style(base_mpf_style='charles',
                          rc={'font.family': 'SimHei'})

mpf.plot(data,
        type='candle',
        title='K线图测试',
        ylabel='价格',
        volume=True,
        style=style)